package net.com.client.test;

import java.io.IOException;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.rabbitmq.client.AMQP.Exchange;
import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class Publisher {

	private static final String EXCHANGE_NAME = "testDurable";
	private static final String ROUNTKEY = "";
	private static final boolean durable = true;

    public static void main(String[] argv)
                  throws Exception {

       createMessage();
    }
    private static void createMessage() {
    	 try{
    		 Date date = new Date();
    		 ConnectionFactory factory = new ConnectionFactory();
    		 factory.setAutomaticRecoveryEnabled(true); //设置网络异常重连
    		 factory.setNetworkRecoveryInterval(10000);//设置 没10s ，重试一次
    		 factory.setTopologyRecoveryEnabled(true);//设置重新声明交换器，队列等信息。
             factory.setHost("localhost");
             factory.setPort(5672);
             factory.setUsername("admin");
             factory.setPassword("admin");
             //factory.setHost("192.168.30.101");
             Connection connection = factory.newConnection();
             Channel channel = connection.createChannel();
             //原来代码
             //channel.exchangeDeclare(EXCHANGE_NAME, "topic",durable);
             //修改之后的代码
             channel.exchangeDeclare(EXCHANGE_NAME, "fanout",durable);
             
             
           //  String message = "{\"index\":"+1+",\"MsgFltTime\":\"2015-07-06 07:50:50\",\"MsgFltType\":\"1\",\"MsgFltSPN\":\"1072\",\"MsgFltFMI\":\"4\",\"MsgFltLo\":100.1201514,\"MsgFltLa\":39.124546,\"VehicleID\":\"154719\"}";
             for(int i=0;i<1;i++){
          		String message = "{\"ModelOperate\":\"提示中心打开"+i+"\",\"browserName\":\"Internet Explorer 11\",\"accountType\":\"0\",\"Account\":\"iemstest\",\"OS\":\"Windows 7\",\"msgTime\":\"2017-05-17 13:34:15\",\"UserName\":\"SuperManager\",\"searchParamString\":{},\"UserIp\":\"192.168.19.51\"}";
          		 channel.basicPublish(EXCHANGE_NAME,"",true,MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));	
          	}
         	  //channel.basicPublish(EXCHANGE_NAME,ROUNTKEY,true,MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
             System.out.println(new Date().getTime() - date.getTime());
             channel.close();
             connection.close();
    	 }catch(ConnectException e){
    		 e.printStackTrace();
    	 } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static String getRouting(String argv) {
		return null;
	}

	private static String getSeverity(String[] argv) {
		return "info0";
	}
	private static String getMessage(String[] strings) {
		if(strings.length<1)return "HelloWorld";
		return joinStrings(strings,"");
	}
	private static String joinStrings(String[] strings, String delimiter) {
		int length = strings.length;
		if(length==0) return "";
		StringBuilder words = new StringBuilder(strings[0]);
		for(int i=1;i<length;i++){
			words.append(strings[i]);
		}
		return words.toString();
	}
}
